#include "dict.h"

using namespace std;

MAP_DICT
sgDict::load(char *dictPath) {
    DICT key, data;
    MAP_DICT map_dict;

    FILE *target = fopen(dictPath, "rb");
    if(target == NULL)
        MYLOG_FATAL << LOAD_DICT_FAILURE;

    int fw = 1;
    int size = 0;
    while(0 != (fw = fread(&size, sizeof(key.dsize), 1, target))) {
        // read form dict file
        if(fw != 1)
            MYLOG_INFO << fw;
        key.dsize = size;

        fw = 0;
        char keybuffer[key.dsize+1];
        bzero(keybuffer, key.dsize+1);
        fw = fread(keybuffer, key.dsize, 1, target);
        key.dptr = keybuffer;
        if(fw != 1)
            MYLOG_INFO << ftell(target);

        fw = 0;
        int fw = fread(&data.dsize, sizeof(data.dsize), 1, target);
        if(fw != 1)
            MYLOG_INFO << ftell(target);

        fw = 0;
        char databuffer[data.dsize+1];
        bzero(databuffer, data.dsize+1);
        fw = fread(databuffer, data.dsize, 1, target);
        data.dptr = databuffer;
        if(fw != 1)
            MYLOG_INFO << ftell(target);

        map_dict.insert(make_pair(key.dptr, data.dptr));
    }

    return map_dict;
}
